/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.bean.inspector;

import com.jb.framework.util.BeanUtil;
import com.jb.util.ClassUtil;
import com.jb.web.bean.FormField;
import com.jb.web.bean.UIComponentImpl;
import com.jb.web.context.WebContext;

import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;


public class PropertyField extends FormField {
    static final long serialVersionUID = 123123L;

    static {
        publish(PropertyField.class, GENERAL, "editorClass");
        publish(PropertyField.class, GENERAL, "mandatory");
    }

    private Object propertyValue;
    private String editorClass;

    private PropertyEditor propertyEditor;


    public void setEditorClass(String editorClass) {
        if( editorClass!=null && editorClass.length()>0 
            && !editorClass.equals(this.editorClass))
        {
            this.editorClass = editorClass;
            propertyEditor=(PropertyEditor)ClassUtil.createInstance(editorClass);
        }else if(editorClass==null || editorClass.length()==0) {
            this.editorClass =null;
            propertyEditor=null;            
        }
    }

    public String getEditorClass() {
        return editorClass;
    }

    public void setPropertyEditor(PropertyEditor pe) {
        this.propertyEditor = pe;
    }

    /**
     * Returns property editor for this filed. It is determined as following.
     * If propertyEditor is aleady assigned, it is returned.
     *
     * If not assigned, propertyValue.getClass is used to get a property editor
     * that is registered PropertyEditorManager
     *
     * If still null, valueOptions is used as a class name of a property editor
     * to create a new propertyEditor.
     *
     *
     * @param pCtx
     * @return
     */
    public PropertyEditor getPropertyEditor(WebContext pCtx) {
        if (propertyEditor != null) {
            return propertyEditor;
        }

        if (propertyEditor == null) {
            propertyEditor = DefaultPropertyEditor.getEditor((PropertyField) this);
            ((DefaultPropertyEditor) propertyEditor).setContext(pCtx);

            return propertyEditor;
        }

        if (propertyValue != null) {
            PropertyEditor epp = PropertyEditorManager.findEditor(propertyValue.getClass());

            if (epp instanceof DefaultPropertyEditor) {
                propertyEditor =  epp;
                ((DefaultPropertyEditor) propertyEditor).setContext(pCtx);
            }
        }

        if (editorClass != null) {
            setEditorClass(editorClass);
            if(propertyEditor instanceof DefaultPropertyEditor)
                ((DefaultPropertyEditor) propertyEditor).setContext(pCtx);
        }
        return propertyEditor;
    }

    public void setPropertyValue(Object propertyValue) {
        this.propertyValue = propertyValue;
    }
}
